package com.dog.basics.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.basics.bean.dto.admin.sys.SysPositionAddDto;
import com.dog.basics.bean.dto.admin.sys.SysPositionEditDto;
import com.dog.basics.bean.model.sys.SysPosition;
import com.dog.basics.bean.vo.admin.sys.SysPositionInfoVo;
import com.dog.basics.bean.vo.admin.sys.SysPositionPageVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.sys.ISysPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;


/**
 * 系统职位模块
 *
 * @author dog
 */
@RestController
@RequestMapping("/sys/position")
@Api(value = SwaggerConstants.SYSTEM+ "系统职位模块", tags = SwaggerConstants.SYSTEM+"系统职位模块")
@AllArgsConstructor
@ApiIgnore
public class SysPositionController {

    private final ISysPositionService sysPositionService;

    /**
     * 系统职位列表
     */
    @ApiOperation(value = "系统职位列表", notes = "sys:position:page")
    @ApiImplicitParam(name = "name", value = "职位名称", required = true, paramType = "String")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('sys:position:page')")
    @Inner
    public R<IPage<SysPositionPageVo>> page(String name, PageParam<SysPosition> page) {
        IPage<SysPositionPageVo> list = sysPositionService.getSysPositionPage(page, name);
        return R.ok(list);
    }

    /**
     * 职位信息
     */
    @ApiOperation(value = "职位信息", notes = "sys:position:info")
    @ApiImplicitParam(name = "id", value = "职位信息ID", required = true, paramType = "Long")
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('sys:position:info')")
    public R<SysPositionInfoVo> info(@PathVariable("id") Long id) {
        SysPosition sysPosition = sysPositionService.getById(id);
        SysPositionInfoVo sysPositionInfoVo = new SysPositionInfoVo();
        BeanUtil.copyProperties(sysPosition, sysPositionInfoVo);
        return R.ok(sysPositionInfoVo);
    }

    /**
     * 保存职位
     */
    @SysLog("保存职位")
    @PostMapping("/save")
    @ApiOperation(value = "保存职位", notes = "sys:position:save")
    @PreAuthorize("@pms.hasPermission('sys:position:save')")
    @Inner
    public R<Boolean> save(@RequestBody @Valid SysPositionAddDto sysPosition) {
        return R.ok(sysPositionService.saveSysPositionAddDto(sysPosition));
    }

    /**
     * 修改配置
     */
    @SysLog("修改职位")
    @ApiOperation(value = "修改职位", notes = "sys:position:update")
    @PutMapping("/update")
    @PreAuthorize("@pms.hasPermission('sys:position:update')")
    @Inner
    public R<Boolean> update(@Valid @RequestBody SysPositionEditDto sysPositionEditDto) {
        return R.ok(sysPositionService.updateSysPositionEditDto(sysPositionEditDto));
    }

    /**
     * 删除配置
     */
    @SysLog("删除职位")
    @ApiOperation(value = "删除职位", notes = "sys:position:delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@pms.hasPermission('sys:position:delete')")
    @Inner
    public R<Boolean> delete(Long[] configIds) {
        sysPositionService.deleteSysPosition(configIds);
        return R.ok();
    }

}
